﻿#include "stdafx.h"
#include "W_CurrentMonitor.h"

#include "Source/MonitorModule/S_MonitorDataContainer.h"
#include "Source/MonitorModule/S_MonitorDisplayingManager.h"
#include "Source/ResultModule/S_ResultWindowManager.h"
#include "Source/ModelingPreModule/S_ModelingPre_InteractiveManager.h"

#include "Source/MainModule/Global/S_FileDefaultDirectory.h"
#include "Source/MainModule/Global/S_TabOrderManager.h"
#include "Source/Utils/Common/TTool.h"

/*
-----==========================================================-----
		类：		电流监视器 编辑窗口.cpp
		作者：		drill_up
		所属模块：	监视器模块
		功能：		电流监视器数据类的编辑窗口。
-----==========================================================-----
*/
W_CurrentMonitor::W_CurrentMonitor(QWidget *parent)
	: W_DialogBase(parent)
{
	ui.setupUi(this);


	//-----------------------------------
	//----参数初始化
	this->m_modelId_list.clear();
	this->m_slotBlock_model = false;


	//-----------------------------------
	//----ui初始化

	// > 窗口初始化
	this->setDataInAddMode();
	this->initDialogButtonBox(ui.buttonBox);


	//-----------------------------------
	//----事件绑定
	connect(ui.lineEdit_x1, &QLineEdit::textEdited, this, &W_CurrentMonitor::refreshModel);
	connect(ui.lineEdit_y1, &QLineEdit::textEdited, this, &W_CurrentMonitor::refreshModel);
	connect(ui.lineEdit_z1, &QLineEdit::textEdited, this, &W_CurrentMonitor::refreshModel);
	connect(ui.lineEdit_x2, &QLineEdit::textEdited, this, &W_CurrentMonitor::refreshModel);
	connect(ui.lineEdit_y2, &QLineEdit::textEdited, this, &W_CurrentMonitor::refreshModel);
	connect(ui.lineEdit_z2, &QLineEdit::textEdited, this, &W_CurrentMonitor::refreshModel);
	connect(ui.lineEdit_x3, &QLineEdit::textEdited, this, &W_CurrentMonitor::refreshModel);
	connect(ui.lineEdit_y3, &QLineEdit::textEdited, this, &W_CurrentMonitor::refreshModel);
	connect(ui.lineEdit_z3, &QLineEdit::textEdited, this, &W_CurrentMonitor::refreshModel);
	connect(ui.checkBox_invertDirection, &QCheckBox::toggled, this, &W_CurrentMonitor::refreshModel);
		
	//-----------------------------------
	//----配置tab顺序
	S_TabOrderManager::getInstance()->configureTabOrder(this);
}
W_CurrentMonitor::~W_CurrentMonitor(){
}
Ui::W_CurrentMonitor *W_CurrentMonitor::getUI(){
	return &ui;
}

/*-------------------------------------------------
		控件 - 获取自动命名
*/
QString W_CurrentMonitor::getAutoName() {
	QString result_name;
	int temp_id = 1;
	while (true) {
		result_name = tr("CurrentMonitor") + QString::number(temp_id);
		if (S_MonitorDataContainer::getInstance()->hasName(result_name) == false) {
			break;
		}else {
			temp_id++; continue;
		}
	}
	return result_name;
}


/*-------------------------------------------------
		模型 - 刷新模型
*/
void W_CurrentMonitor::refreshModel() {
	if (this->m_slotBlock_model == true) { return; }

	// > 若已存在，则删除
	if (this->m_modelId_list.count() > 0) {
		for (int i = 0; i < this->m_modelId_list.count(); i++) {
			QString model_id = this->m_modelId_list.at(i);
			S_ModelingPre_InteractiveManager::getInstance()->model_destory(model_id, false);
		}
		this->m_modelId_list.clear();
	}

	// > 临时创建
	double x1 = ui.lineEdit_x1->text().toDouble();
	double y1 = ui.lineEdit_y1->text().toDouble();
	double z1 = ui.lineEdit_z1->text().toDouble();
	double x2 = ui.lineEdit_x2->text().toDouble();
	double y2 = ui.lineEdit_y2->text().toDouble();
	double z2 = ui.lineEdit_z2->text().toDouble();
	double x3 = ui.lineEdit_x3->text().toDouble();
	double y3 = ui.lineEdit_y3->text().toDouble();
	double z3 = ui.lineEdit_z3->text().toDouble();
	gp_Pnt p1 = gp_Pnt(x1, y1, z1);
	gp_Pnt p2 = gp_Pnt(x2, y2, z2);
	gp_Pnt p3 = gp_Pnt(x3, y3, z3);

	// > 错误的线（出现三点共线时）
	if (C_FieldMonitor::util_isCollinear({ x1, y1, z1 }, { x2, y2, z2 }, { x3, y3, z3 }) == true) {
		QList<gp_Pnt> p_list;
		p_list.append(p1);
		p_list.append(p2);
		p_list.append(p3);
		QString model_id = S_ModelingPre_InteractiveManager::getInstance()->model_CreateMultiLine(p_list, QColor(205, 55, 0), 4);	//橙色线
		this->m_modelId_list.append(model_id);
		return;
	}

	// > 正确的线
	DSEM::Point3D temp_p_4 = C_FieldMonitor::util_getQuadrilateralVertex({ x1, y1, z1 }, { x2, y2, z2 }, { x3, y3, z3 });
	gp_Pnt p4 = gp_Pnt(temp_p_4.X(), temp_p_4.Y(), temp_p_4.Z());

	bool invertDirection = ui.checkBox_invertDirection->isChecked();
	if (invertDirection == false) {
		QString model_id_1 = S_ModelingPre_InteractiveManager::getInstance()->model_CreateDiscretePort(p1, p2, QColor(205, 55, 0), QColor(205, 55, 0), false);
		QString model_id_2 = S_ModelingPre_InteractiveManager::getInstance()->model_CreateDiscretePort(p2, p3, QColor(205, 55, 0), QColor(205, 55, 0), false);
		QString model_id_3 = S_ModelingPre_InteractiveManager::getInstance()->model_CreateDiscretePort(p3, p4, QColor(205, 55, 0), QColor(205, 55, 0), false);
		QString model_id_4 = S_ModelingPre_InteractiveManager::getInstance()->model_CreateDiscretePort(p4, p1, QColor(205, 55, 0), QColor(205, 55, 0), false);
		this->m_modelId_list.append(model_id_1);
		this->m_modelId_list.append(model_id_2);
		this->m_modelId_list.append(model_id_3);
		this->m_modelId_list.append(model_id_4);
	}else{
		QString model_id_1 = S_ModelingPre_InteractiveManager::getInstance()->model_CreateDiscretePort(p1, p4, QColor(205, 55, 0), QColor(205, 55, 0), false);
		QString model_id_2 = S_ModelingPre_InteractiveManager::getInstance()->model_CreateDiscretePort(p4, p3, QColor(205, 55, 0), QColor(205, 55, 0), false);
		QString model_id_3 = S_ModelingPre_InteractiveManager::getInstance()->model_CreateDiscretePort(p3, p2, QColor(205, 55, 0), QColor(205, 55, 0), false);
		QString model_id_4 = S_ModelingPre_InteractiveManager::getInstance()->model_CreateDiscretePort(p2, p1, QColor(205, 55, 0), QColor(205, 55, 0), false);
		this->m_modelId_list.append(model_id_1);
		this->m_modelId_list.append(model_id_2);
		this->m_modelId_list.append(model_id_3);
		this->m_modelId_list.append(model_id_4);
	}
	S_ModelingPre_InteractiveManager::getInstance()->updateViewModeling();

}
/*-------------------------------------------------
		模型 - 删除模型
*/
void W_CurrentMonitor::clearModel() {
	for (int i = 0; i < this->m_modelId_list.count(); i++) {
		QString model_id = this->m_modelId_list.at(i);
		S_ModelingPre_InteractiveManager::getInstance()->model_destory(model_id, false);
	}
	this->m_modelId_list.clear();
	S_ModelingPre_InteractiveManager::getInstance()->updateViewModeling();
}


/*-------------------------------------------------
		窗口 - 设置两点的数据
*/
void W_CurrentMonitor::setDataPoint(QList<double> p1, QList<double> p2, QList<double> p3) {
	if (p1.count() < 3) { return; }
	ui.lineEdit_x1->setText(QString::number(p1.at(0)));
	ui.lineEdit_y1->setText(QString::number(p1.at(1)));
	ui.lineEdit_z1->setText(QString::number(p1.at(2)));
	ui.checkBox_usePick->setChecked(true);

	if (p2.count() < 3) { return; }
	ui.lineEdit_x2->setText(QString::number(p2.at(0)));
	ui.lineEdit_y2->setText(QString::number(p2.at(1)));
	ui.lineEdit_z2->setText(QString::number(p2.at(2)));
	ui.checkBox_usePick_2->setChecked(true);

	if (p3.count() < 3) { return; }
	ui.lineEdit_x3->setText(QString::number(p3.at(0)));
	ui.lineEdit_y3->setText(QString::number(p3.at(1)));
	ui.lineEdit_z3->setText(QString::number(p3.at(2)));
	ui.checkBox_usePick_3->setChecked(true);

	// > 刷新模型
	this->refreshModel();
}
/*-------------------------------------------------
		窗口 - 设置数据（添加）
*/
void W_CurrentMonitor::setDataInAddMode() {
	this->local_CurrentMonitor.init();
	this->isAddMode = true;
	this->putDataToUi();

	// > 自动命名
	ui.lineEdit_name->setText(this->getAutoName());
}
/*-------------------------------------------------
		窗口 - 设置数据（修改）
*/
void W_CurrentMonitor::setDataInModifyMode(C_CurrentMonitor p) {
	this->local_CurrentMonitor = p;
	this->isAddMode = false;
	this->putDataToUi();
}

/*-------------------------------------------------
		窗口 - 取出数据
*/
C_CurrentMonitor W_CurrentMonitor::getData(){
	return this->local_CurrentMonitor;
};
/*-------------------------------------------------
		窗口 - 本地数据 -> ui数据
*/
void W_CurrentMonitor::putDataToUi() {
	this->m_slotBlock_model = true;

	ui.lineEdit_name->setText(this->local_CurrentMonitor.getName());

	TTool::_double_(ui.lineEdit_x1, &this->local_CurrentMonitor.x1);
	TTool::_double_(ui.lineEdit_y1, &this->local_CurrentMonitor.y1);
	TTool::_double_(ui.lineEdit_z1, &this->local_CurrentMonitor.z1);
	TTool::_double_(ui.lineEdit_x2, &this->local_CurrentMonitor.x2);
	TTool::_double_(ui.lineEdit_y2, &this->local_CurrentMonitor.y2);
	TTool::_double_(ui.lineEdit_z2, &this->local_CurrentMonitor.z2);
	TTool::_double_(ui.lineEdit_x3, &this->local_CurrentMonitor.x3);
	TTool::_double_(ui.lineEdit_y3, &this->local_CurrentMonitor.y3);
	TTool::_double_(ui.lineEdit_z3, &this->local_CurrentMonitor.z3);
	ui.checkBox_invertDirection->setChecked(this->local_CurrentMonitor.directionInverted);

	this->m_slotBlock_model = false;
	this->refreshModel();
};
/*-------------------------------------------------
		窗口 - ui数据 -> 本地数据
*/
void W_CurrentMonitor::putUiToData() {

	this->local_CurrentMonitor.setName(ui.lineEdit_name->text());

	TTool::_double_(&this->local_CurrentMonitor.x1, ui.lineEdit_x1);
	TTool::_double_(&this->local_CurrentMonitor.y1, ui.lineEdit_y1);
	TTool::_double_(&this->local_CurrentMonitor.z1, ui.lineEdit_z1);
	TTool::_double_(&this->local_CurrentMonitor.x2, ui.lineEdit_x2);
	TTool::_double_(&this->local_CurrentMonitor.y2, ui.lineEdit_y2);
	TTool::_double_(&this->local_CurrentMonitor.z2, ui.lineEdit_z2);
	TTool::_double_(&this->local_CurrentMonitor.x3, ui.lineEdit_x3);
	TTool::_double_(&this->local_CurrentMonitor.y3, ui.lineEdit_y3);
	TTool::_double_(&this->local_CurrentMonitor.z3, ui.lineEdit_z3);
	this->local_CurrentMonitor.directionInverted = ui.checkBox_invertDirection->isChecked();
};
/*-------------------------------------------------
		窗口 - 提交数据（校验）
*/
void W_CurrentMonitor::base_acceptData(){
	this->putUiToData();

	// > 校验 - 名称
	QString name = this->local_CurrentMonitor.getName();
	if ( name == "" ){
		QMessageBox::warning(this, tr("提示"), tr("名称不能为空。"));
		return;
	}
	if (this->isAddMode == true && S_MonitorDataContainer::getInstance()->hasName(name)) {
		QMessageBox::warning(this, tr("提示"), tr("已有此名称，不可重复。"));
		return;
	}

	// > 校验 - 三点共线
	if (this->local_CurrentMonitor.isCollinear()) {
		QMessageBox::warning(this, tr("提示"), tr("三点不能共线。"));
		return;
	}

	// > 校验 - 操作是否与结果文件冲突（含迁移处理）
	if (S_ResultWindowManager::getInstance()->isConflictToResult_WithMigrateTip()) {
		return;
	}

	emit acceptDataSignal(this->isAddMode, this->local_CurrentMonitor);
	W_DialogBase::base_acceptData();

	// > 清理模型
	this->clearModel();
};
/*-------------------------------------------------
		窗口 - 取消数据（校验）
*/
void W_CurrentMonitor::base_rejectData() {
	W_DialogBase::base_rejectData();

	// > 清理模型
	this->clearModel();

	// > 重新显示旧模型
	if (this->isAddMode == false) {
		S_MonitorDisplayingManager::getInstance()->refreshModel_NoUpdate_CurrentMonitor(this->local_CurrentMonitor.getId());
		S_ModelingPre_InteractiveManager::getInstance()->updateViewModeling();
	}
}
/*-------------------------------------------------
		窗口 - 关闭事件
*/
void W_CurrentMonitor::closeEvent(QCloseEvent *e) {
	W_DialogBase::closeEvent(e);
	
	// > 清理模型
	this->clearModel();
}